package com.jeeplus.mob.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.mob.common.DataConvertHelper;
import com.jeeplus.mob.entity.mobpojo.BuildingcardrecordInfo;
import com.jeeplus.mob.entity.mobpojo.WaycardrecordInfo;
import com.jeeplus.mob.mapper.BuildingcardrecordInfoMapper;
import com.jeeplus.mob.mapper.CardrecordInfoMapper;
import com.jeeplus.mob.mapper.WaycardrecordInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
public class CardrecordService {

    @Autowired
    private CardrecordInfoMapper cardrecordInfoMapper;

    @Autowired
    private WaycardrecordInfoMapper waycardrecordInfoMapper;

    @Autowired
    private BuildingcardrecordInfoMapper buildingcardrecordInfoMapper;

//    public AjaxJson punch(CardrecordInfo cardrecordInfo) {
//
//        return new AjaxJson();
//    }
//
//    /**
//     * 在途打卡，逻辑处理（判断第一次 or 第二次打卡做不同的处理）
//     * @param cardrecordInfo
//     * @return
//     */
//    public AjaxJson punchWay(CardrecordInfo cardrecordInfo) {
//        Date punchStartDate = cardrecordInfo.getPunchStartDate();  // 获取打卡时间
//        boolean flag = DateUtils.dateCompare(punchStartDate,new Date());  // 返回true，说明传入日期大于当前日期
//        // 根据用户ID、楼宇ID查询当前用户是否为第二次在途打卡
//        CardrecordInfo cardrecordInfomation = cardrecordInfoMapper.selectCardrecordInfoByUserIdAndBuildingId(cardrecordInfo.getUserId(), cardrecordInfo.getBuildingId());
//        // 判断当前对象是否为空，如果不为空则增加在途打卡结束打卡时间，并计算第一次和第二次打卡间隔时间
//        if (!flag && !StringHelper.IsEmptyOrNull(cardrecordInfomation) ) {
//            int rows = cardrecordInfoMapper.updateBuildingIntervalTime(cardrecordInfomation);
//            if (rows > 0) {
//                return AjaxJson.success("打卡成功！");
//            } else {
//                return AjaxJson.success("打卡未成功，请重试！");
//            }
//        }
//
//        // 没有空的结束打卡时间则直接打卡
//        int rows = cardrecordInfoMapper.addPunchWay(cardrecordInfo);
//        if (rows > 0 ) {
//            return AjaxJson.success("打卡成功！");
//        } else {
//            return AjaxJson.success("打卡未成功，请重试！");
//        }
//    }
//
//    public AjaxJson punchBuilding(CardrecordInfo cardrecordInfo) {
//        int rows = cardrecordInfoMapper.addPunchBuilding(cardrecordInfo);  // 打卡记录表插入打卡信息
//        if (rows > 0) {
//            return AjaxJson.success("打卡成功！");
//        }
//        return AjaxJson.success("打卡未成功，请重试！");
//    }
//
//    /**
//     * 根据用户打卡日期获取打卡数据统计
//     * @param request
//     * @param response
//     * @return
//     */
//    public AjaxJson getPunchDataStatistics (HttpServletRequest request,HttpServletResponse response) {
//        Map<String, Object> requestParams = DataConvertHelper.getRequestParamsNoToken(request);
//        if (!requestParams.containsKey("userId")) {
//            return AjaxJson.success("缺少用户ID参数！");
//        }
//        if (!requestParams.containsKey("punchTime")) {
//            return AjaxJson.success("缺少查询日期参数！");
//        }
//        List<Map<String, Object>> punchDataStatistics = cardrecordInfoMapper.getPunchDataStatistics(requestParams);
//        if (punchDataStatistics.size() > 0 && StringHelper.IsEmptyOrNull(punchDataStatistics)) {
//
//        }
//        return AjaxJson.success().put("data",punchDataStatistics);
//    }

    /*public AjaxJson getPunchDataStatistics (HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> requestParams = DataConvertHelper.getRequestParamsNoToken(request);
        List<Map<String, Object>> punchDataStatistics = cardrecordInfoMapper.getPunchDataStatistics(requestParams);

        StringBuffer wayPunchId = new StringBuffer("");
        StringBuffer buildingPunchId = new StringBuffer("");
        if (punchDataStatistics.size() > 0) {
            for (int i = 0; i < punchDataStatistics.size(); i++) {
                if (i < punchDataStatistics.size() - 1) {
                    wayPunchId.append(punchDataStatistics.get(i).get("wayPunchId") + ",");
                    buildingPunchId.append(punchDataStatistics.get(i).get("buildingPunchId") + ",");
                }
                wayPunchId.append(punchDataStatistics.get(i).get("wayPunchId"));
                buildingPunchId.append(punchDataStatistics.get(i).get("buildingPunchId"));
            }
        }
        List<WaycardrecordInfo> waycardrecordInfoByIds = waycardrecordInfoMapper.getWaycardrecordInfoByIds(wayPunchId.toString());
        List<BuildingcardrecordInfo> buildingcardrecordInfoByIds = buildingcardrecordInfoMapper.getBuildingcardrecordInfoByIds(buildingPunchId.toString());
        if (waycardrecordInfoByIds.size() > 0 && buildingcardrecordInfoByIds.size() > 0) {
            return AjaxJson.success().put("wayPunch",waycardrecordInfoByIds).put("buildingPunch",buildingcardrecordInfoByIds);
        }
        return AjaxJson.success("暂无打卡数据！");
    }*/

    public AjaxJson getPunchDataStatistics (HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> requestParams = DataConvertHelper.getRequestParamsNoToken(request);
        List<BuildingcardrecordInfo> buildingcardrecordInfoByUserDate = buildingcardrecordInfoMapper.getBuildingcardrecordInfoByUserDate(requestParams);
        List<WaycardrecordInfo> waycardrecordInfoByUserDate = waycardrecordInfoMapper.getWaycardrecordInfoByUserDate(requestParams);
        return AjaxJson.success().put("buildingCardrecord",buildingcardrecordInfoByUserDate).put("wayCardrecord",waycardrecordInfoByUserDate);
    }
}
